package com.org.cys.util;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
//import com.gdcirrus.common.util.collections.CollectionUtil;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 处理返回json格式时，空串和日期的格式化
 * @author caiyushen
 * */
public class JsonDataMapping extends ObjectMapper {
    private static final long serialVersionUID = 5605424237698352497L;
    private Boolean hibernateLazyHandler;
    private List<Module> registerModules;

    public List<Module> getRegisterModules() {
        return this.registerModules;
    }

    public void setRegisterModules(List<Module> registerModules) {
        this.registerModules = registerModules;
    }

    public Boolean getHibernateLazyHandler() {
        return this.hibernateLazyHandler;
    }

    public void setHibernateLazyHandler(Boolean hibernateLazyHandler) {
        this.hibernateLazyHandler = hibernateLazyHandler;
    }

    public JsonDataMapping() {
        this(Boolean.valueOf(false));
    }

    public JsonDataMapping(Boolean hibernateLazyHandler) {
        this(hibernateLazyHandler, (List)null);
    }

    public JsonDataMapping(Boolean hibernateLazyHandler, List<Module> registerModules) {
        this.hibernateLazyHandler = Boolean.valueOf(false);
        this.hibernateLazyHandler = hibernateLazyHandler;
        this.registerModules = registerModules;
        this.hibernateLazyHandler = hibernateLazyHandler;
        this.configure(Feature.ALLOW_COMMENTS, true);
        this.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        this.configure(SerializationFeature.WRITE_ENUMS_USING_INDEX, true);
        this.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        this.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, true);
        this.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
        this.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        /*if(CollectionUtil.isNotEmpty(registerModules)) {
            registerModules.forEach(this::registerModule);
        }*/

        this.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
            public void serialize(Object value, JsonGenerator jg, SerializerProvider sp) throws IOException {
                jg.writeString("");
            }
        });
    }
}
